﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PictureResizerForms
{
    public class PictureResize
    {

        public Image ScaleImage(Image image, int maxHeight, int maxWidth)
        {
            double ratioX, ratioY, ratio;

            try
            {
                ratioX = (double)maxWidth / image.Width;
                ratioY = (double)maxHeight / image.Height;
                ratio = Math.Min(ratioX, ratioY);
            }
            catch (System.DivideByZeroException dbz)
            {
                System.Console.WriteLine("Image width or height was zero.");
                return null;
            }

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }


    }
}
